package com.jspgou.cms.manager.impl;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jspgou.common.hibernate3.Updater;
import com.jspgou.common.page.Pagination;
import com.jspgou.core.entity.Website;
import com.jspgou.core.manager.WebsiteMng;
import com.jspgou.core.resolver.RealPathResolver;
import com.jspgou.cms.dao.CouponDao;
import com.jspgou.cms.dao.MemberCouponDao;
import com.jspgou.cms.entity.Coupon;
import com.jspgou.cms.entity.MemberCoupon;
import com.jspgou.cms.entity.ProductType;
import com.jspgou.cms.manager.CouponMng;
import com.jspgou.cms.manager.ProductTypeMng;
import com.jspgou.cms.manager.ShopMemberMng;

@Service
@Transactional
public class CouponMngImpl implements CouponMng {
	@Transactional(readOnly = true)
	public Pagination getPage(Long storeId,int pageNo, int pageSize) {
		Pagination page = dao.getPage(storeId,pageNo, pageSize);
		return page;
	}
	
	
	@Transactional(readOnly = true)
	public List<Coupon> getList(Long storeId) {
		List<Coupon> list = dao.getList(storeId);
		return list;
	}

	
	@Transactional(readOnly = true)
	public Coupon findById(Long id) {
		Coupon entity = dao.findById(id);
		return entity;
	}

	public Coupon save(Coupon bean,Long webId,Long pTypeid) {
		Website site=siteMng.findById(webId);
		bean.setWebsite(site);
		/*ProductType pType=ptypeMng.findById(pTypeid);
		bean.setProductType(pType);*/
		Coupon entity = dao.save(bean);
		
		return entity;
	}

	public Coupon update(Coupon bean,Long pTypeid) {
	/*	bean.setProductType(ptypeMng.findById(pTypeid));*/
		Updater<Coupon> updater = new Updater<Coupon>(bean);
		Coupon entity = dao.updateByUpdater(updater);
		
		return entity;
	}
	
	public void fetchCoupon(Double bd,Long memberId){
		Coupon bean= dao.fetchCoupon(bd);
		Long time=new Date().getTime();
		String str=time.intValue()+"";
		String mestr=memberId.intValue()+"";
		str=str.substring(str.length()-6, str.length());
		if(bean != null){
			MemberCoupon mc=new MemberCoupon();
			mc.setCode(str+"-"+mestr);
			mc.setCoupon(bean);
			mc.setIsuse(false);
			mc.setMember(memberMng.findById(memberId));
			memberCouponMng.save(mc);
		}
		
	}

	public Coupon deleteById(Long id,String url) {
		
		Coupon entity = findById(id);
		/*String path=entity.getCouponPicture();
		String path1 = realPathResolver.get(path).replace("\\", File.separator)
				.replace("/", File.separator).replace(
						url.replace("\\", File.separator).replace("/",
								File.separator)+url.replace("\\", File.separator).replace("/",
										File.separator), url.replace("\\", File.separator).replace("/",
												File.separator));
		File f=new File(path1);
		if(f!=null){
			f.delete();
		}*/
		entity = dao.deleteById(id);
		return entity;
	}

	public Coupon[] deleteByIds(Long[] ids,String url) {
		Coupon[] beans = new Coupon[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i],url);
		}
		return beans;
	}
	
	@Autowired
	private RealPathResolver realPathResolver;
	@Autowired
	private ProductTypeMng ptypeMng;
	@Autowired
	private CouponDao dao;
	@Autowired
	private WebsiteMng siteMng;
	@Autowired
	private ShopMemberMng memberMng;
	@Autowired
	private MemberCouponDao memberCouponMng;
}